package cn.wolfcode.luowowo.article.service.impl;

import cn.wolfcode.luowowo.article.domain.ScenicDetail;
import cn.wolfcode.luowowo.article.mapper.ScenicDetailMapper;
import cn.wolfcode.luowowo.article.service.IScenicDetailService;
import cn.wolfcode.luowowo.common.query.ScenicQueryObject;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

@Service
public class ScenicDetailServiceImpl implements IScenicDetailService {

    @Autowired
    private ScenicDetailMapper scenicDetailMapper;

    public List<ScenicDetail> listScenicTop3(Long destId) {
        return scenicDetailMapper.selectTop3ByDestId(destId);
    }

    public ScenicDetail getById(Long scenicId) {
        return scenicDetailMapper.selectByPrimaryKey(scenicId);
    }

    public void updateSumById(Long scenicId, int num) {
        scenicDetailMapper.updateSumById(scenicId,num);
    }

    public List<ScenicDetail> listHotScenic(Long destId) {
        return scenicDetailMapper.listHotScenic(destId);
    }

    public PageInfo<ScenicDetail> query(ScenicQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),15);
        if(qo.getTagid() == -1) {
            List<ScenicDetail> list = scenicDetailMapper.selectAll();
            return new PageInfo<ScenicDetail>(list);
        }else{
            List<ScenicDetail> list = scenicDetailMapper.listByTagid(qo.getTagid());
            return new PageInfo<ScenicDetail>(list);
        }
    }

    public AjaxResult favor(Long sceincId, Long userId) {
        AjaxResult result = new AjaxResult();
        try{
            int num = 1;
            // 判断当前用户是否收藏了该景点, 如果收藏了, 就取消, 景点收藏数-1,
            // 没有收藏就添加收藏, 景点收藏数+1
            List<ScenicDetail> list = scenicDetailMapper.judge(sceincId, userId);
            if(list.size() == 0){
                //用户没有收藏, 中间表要添加关联数据
                scenicDetailMapper.favor(sceincId,num);
                scenicDetailMapper.insertRelation(sceincId,userId);
                result.setMsg("收藏成功");
            }else {
                //用户已经收藏过了, 此操作是取消收藏, 中间表也要删除关联数据
                num = -1;
                scenicDetailMapper.favor(sceincId,num);
                scenicDetailMapper.deleteRelation(sceincId,userId);
                result.setMsg("已取消收藏");
            }
        }catch (Exception e){
            e.printStackTrace();
            result.mark("收藏失败");
        }
        return result;
    }

    public AjaxResult overgo(Long scenicId) {
        AjaxResult result = new AjaxResult();
        try{
            int num = 1;
            //景点去过数+1
            scenicDetailMapper.overgo(scenicId,num);
            result.setMsg("操作成功");
        }catch (Exception e){
            e.printStackTrace();
            result.mark("操作失败");
        }
        return result;
    }

    public List<ScenicDetail> getChilPage(Long scenicId, int page) {
        ScenicDetail scenicDetail = scenicDetailMapper.selectByPrimaryKey(scenicId);
        List<ScenicDetail> children = scenicDetail.getChildren();
        int fromIndex = page * 4 - 4;
        int toIndex =fromIndex + 4;
        int length = children.size();
        if (length >= toIndex + 1) {
            children = children.subList(fromIndex, toIndex);
        }else if (length >= fromIndex + 1 && length <= toIndex + 1){
            children = children.subList(fromIndex, length);
        }else{
            children = new ArrayList<>();
        }
        return children;
    }
}
